import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

            int n = in.nextInt();
            int m = in.nextInt();

            char []s1=new char[n+1];
            char []s2=new char[m+1];

            for (int i=1;i<=n;i++){
                s1[i]=in.next().charAt(0);
            }
        for (int i=1;i<=m;i++){
            s2[i]=in.next().charAt(0);
        }

        int [][]dp=new int[n+1][m+1];

        for (int i=1;i<=n;i++){
            for (int j=1;j<=m;j++){
                if (s1[i]==s2[j]){
                    dp[i][j]=dp[i-1][j-1]+1;
                }else {
                    dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
            System.out.println(dp[n][m]);
        in.close();
        }
    }
